package com.dff.jrfw;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;

import com.dff.jrfw.common.Constant;
import com.dff.jrfw.databinding.ActivityModifyPayPsdBinding;
import com.dff.jrfw.utils.SharedPreferenceUtils;
import com.dff.jrfw.utils.ToastUtil;
import com.dff.jrfw.utils.Util;

import cn.com.infosec.mobile.android.managers.CertManager;
import cn.com.infosec.mobile.android.result.Result;

public class ModifyPayPsdActivity extends BaseActivity implements View.OnClickListener {

    private ActivityModifyPayPsdBinding binding;
    private int psdVisible = 0;    //默认密码不可见

    public static void callMe(Context context) {
        Intent intent = new Intent(context, ModifyPayPsdActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_modify_pay_psd);
        initView();
    }


    private void initView() {
        binding.ivBack.setOnClickListener(this);
        binding.ivEye.setOnClickListener(this);
        binding.btnNext.setOnClickListener(this);
        binding.tvForgotPayPsd.setOnClickListener(this);
        binding.ivInputClearPsd.setOnClickListener(this);
        binding.etPayPsd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String etNewPsd = charSequence.toString().trim();
                if (etNewPsd.length() == 0) {
                    binding.ivInputClearPsd.setVisibility(View.INVISIBLE);
                } else {
                    binding.ivInputClearPsd.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_next:
                String etPayPsd = binding.etPayPsd.getText().toString().trim();
                if (TextUtils.isEmpty(etPayPsd)) {
                    ToastUtil.toast("请输入支付密码");
                    return;
                }
                String curLoginAccount = (String) SharedPreferenceUtils.getInstance().get(MyApplication.getContext(), Constant.CURRENT_LOGIN_ACCOUNT, "");
                goCert(curLoginAccount, etPayPsd);
                Util.hideKeyBoard(binding.btnNext);
                break;
            case R.id.tv_forgot_pay_psd:
                ForgotPayPsdActivity.callMe(ModifyPayPsdActivity.this);
                break;
            case R.id.iv_input_clear_psd:
                binding.etPayPsd.setText("");
                break;
            case R.id.iv_eye:
                if (psdVisible == 0) {
                    psdVisible = 1;
                    binding.ivEye.setImageResource(R.drawable.open_eye);
                    binding.etPayPsd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    psdVisible = 0;
                    binding.ivEye.setImageResource(R.drawable.cl_eye);
                    binding.etPayPsd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
        }
    }


    private void goCert(final String userName, final String PIN) {
        showLoading();
        // 检查密码是否正确
        CertManager certManager = new CertManager(this);
        if (certManager.isCertExist(userName)) {
            certManager.verifyPIN(userName, PIN, new Result.ResultListener() {
                @Override
                public void handleResult(Result result) {
                    hideLoading();
                    if (TextUtils.equals(Result.OPERATION_SUCCEED, result.getResultID())) {
                        // PIN正确
                        ModifyPayPsdStepTwoActivity.callMe(ModifyPayPsdActivity.this, userName, PIN, "", "modify");
                    } else if (TextUtils.equals(Result.INCORRECT_CERTPIN, result.getResultID())) {
                        int retryTimes = Integer.parseInt(result.getResultDesc());
                        // 密码错误，提示用户还有几次机会
                        Toast.makeText(ModifyPayPsdActivity.this, "支付密码校验失败，剩余" + retryTimes + "次尝试次数", Toast.LENGTH_SHORT).show();
                    } else if (TextUtils.equals(Result.USER_LOCKED, result.getResultID())) {
                        // 尝试次数已经达到最大，已经锁定用户
                        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ModifyPayPsdActivity.this);
                        sharedPreferences.edit().putBoolean("userLock", true).apply();
                        ToastUtil.toast("账户锁定，请联系管理员");
                    } else {
                        // 发生了其他错误
                        Toast.makeText(ModifyPayPsdActivity.this, result.toString(), Toast.LENGTH_SHORT).show();
                    }
                }
            });
        } else {
            hideLoading();
            ToastUtil.toast("证书不存在");
        }
    }


}